#include "testfootmark.h"


#include <QDate>
#include <QTime>
#include <QDateTime>

#include <QUuid>
#include <QRandomGenerator>


namespace kepen {
namespace core {

static bool isRunNian(int year) {

    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return true;
    } else {
        return false;
    }
}

void randomDaytime(char buf[])
{
    int hh,mm,ss;
    int day = 0;
    int year = QRandomGenerator::global()->bounded(50);
    int month = QRandomGenerator::global()->bounded(12);

    year += 1970;

    ++month;

    if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month || 12 == month) {
        day = QRandomGenerator::global()->bounded(31);
    } else if (2 == month) {
        if (isRunNian(year)) {
            day = QRandomGenerator::global()->bounded(29);
        } else {
            day = QRandomGenerator::global()->bounded(28);
        }

    } else {
        day = QRandomGenerator::global()->bounded(30);
    }

    ++day;

    hh = QRandomGenerator::global()->bounded(24);
    mm = QRandomGenerator::global()->bounded(60);
    ss = QRandomGenerator::global()->bounded(60);
    sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d", year, month, day,hh,mm,ss);
}

TestFootmark::TestFootmark() {
    ng = new kepen::kemod::NameGenerator();
}

TestFootmark::~TestFootmark()
{
    delete ng;
}

void TestFootmark::initNode(kemod::KeFootmark *node)
{

    char dataTime[32]={0};

    const char *addS[]={"穗丰园"
                          ,"塞浦路斯"
                          ,"果香园"
                          ,"迎宾园"
                          ,"启明里"
                          ,"别伊斯"
    ,"书香门第","毓秀里","龙溪里","东秀苑"};
    double coords[]={84.39,41.41};
    double fv = 2.0;
    QUuid uniqueId = QUuid::createUuid();
    node->id = uniqueId.toString(QUuid::StringFormat::WithoutBraces);
    int idxHouse = QRandomGenerator::global()->bounded(40);
    double lng = QRandomGenerator::global()->bounded(fv);
    double lat = QRandomGenerator::global()->bounded(fv);
    qsizetype asize=0;
    const ushort* nameRand= ng->next(&asize);
    QString qsName;
    qsName.setUtf16(nameRand,asize);
    // qDebug() .noquote() <<__FILE__ << __LINE__ << " node->id  : " << node->id  <<(node->id.length());
    int d ;
    int maxC = sizeof(addS)/sizeof(addS[0]);
    d= QRandomGenerator::global()->bounded(maxC);
    if(d>=maxC){
        d=0;
    }

    randomDaytime(dataTime);

    node->name = qsName;
    node->address = QString::asprintf("%s%d栋",addS[d],idxHouse);
    node->level=18;
    node->lng=coords[0]+lng;
    node->lat=coords[1]+lat;
    node->createTime = QString(dataTime);
    node->updateTime = QString(dataTime);
    node->version = QRandomGenerator::global()->bounded(40);


}

void TestFootmark::buildArray(std::list<std::unique_ptr<kemod::KeFootmark>> *vs)
{
    std::unique_ptr<kemod::KeFootmark> node(std::make_unique<kemod::KeFootmark>());
    initNode(node.get());
    vs->push_back(std::move(node));
}


}
}
